package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_advpos")
@Slf4j
public class YddAdvposController {

    @Autowired
    private IYddAdvposService yddAdvposService;

    @Autowired
    private IYddAdvimgService yddAdvimgService;

    @GetMapping("/list")
    public R page(String advposdesc,String advposname,Integer page,Integer limit){
        //分页构造器
        Page<YddAdvpos> pageInfo=new Page<>(page,limit);
        LambdaQueryWrapper<YddAdvpos> advposLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //将传入的参数添加进条件选择器
        advposLambdaQueryWrapper.like(Strings.isNotEmpty(advposdesc),YddAdvpos::getAdvposdesc,advposdesc).like(Strings.isNotEmpty(advposname),YddAdvpos::getAdvposname,advposname);
        yddAdvposService.page(pageInfo,advposLambdaQueryWrapper);

        List<YddAdvposDto> data=new ArrayList<>();

        //根据广告位id查询,广告图的数量
        for (YddAdvpos yddAdvpos : pageInfo.getRecords()) {
            YddAdvposDto yddAdvposDto=new YddAdvposDto();
            BeanUtils.copyProperties(yddAdvpos,yddAdvposDto);
            LambdaQueryWrapper<YddAdvimg> advimgQueryWrapper=new LambdaQueryWrapper<>();
            advimgQueryWrapper.eq(YddAdvimg::getAdvimgpos,yddAdvpos.getId());
            int count = yddAdvimgService.count(advimgQueryWrapper);
            yddAdvposDto.setAdvposnum(count);
            data.add(yddAdvposDto);
        }

        R r=new R();
        r.putData("count",Integer.parseInt(String.valueOf(pageInfo.getTotal())));
        r.putData("totalPages",(int)Math.ceil((Double.valueOf(String.valueOf(pageInfo.getTotal()))/pageInfo.getSize())));
        r.putData("pageSize",Integer.parseInt(String.valueOf(pageInfo.getSize())));
        r.putData("currentPage",Integer.parseInt(String.valueOf(pageInfo.getCurrent())));
        r.putData("data",data);

        r.setErrmsg("");
        r.setErrno(0);
        log.info(r.toString());
        return r;
    }

    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id){
        YddAdvpos advpos = yddAdvposService.getById(id);
        return ResultData.ok("",advpos);
    }

    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvpos yddAdvpos){
        yddAdvposService.updateById(yddAdvpos);
        return ResultData.ok("","");
    }

    @GetMapping("/del")
    public ResultData delete(Integer id){
        yddAdvposService.removeById(id);
        return ResultData.ok("","");
    }

    @GetMapping("/all")
    public ResultData all(){
        List<YddAdvpos> list = yddAdvposService.list();
        return ResultData.ok("",list);
    }

    @PostMapping("/add")
    public ResultData add(@RequestBody YddAdvpos yddAdvpos){
        yddAdvposService.save(yddAdvpos);
        return ResultData.ok("",yddAdvpos);
    }
}
